<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class AdController extends Controller
{
    //index
    public function index()
    {
    	$data = DB::table('ad') -> orderBy('id', 'desc') -> first();
    	return view('admin.ad.index',['data' => $data]);
    }

    //add
    public function add(Request $request)
    {
    	$data = $request -> except('_token');
    	if($request -> hasFile('adimg'))
        {
            if($request -> file('adimg') -> isValid())
            {
                //移动文件
                $suffix = $request -> file('adimg') -> getClientOriginalExtension();
                //随机一个文件名
                $filename = time().mt_rand(100000,9999999);

                //移动文件
                $request -> file('adimg') -> move('./uploads', $filename.'.'.$suffix);
                $data['adimg'] = $filename.'.'.$suffix;
               
            }

        }
        
        if($data['url']!='')
        {
        	$res = DB::table('ad')->insert($data);
        }else
        {
        	return back() -> with(['info' => '请上传图片和URL地址']);
        }
        
        
        
    }
}
